﻿#include "mysql.h"

mySql::mySql()
{
        if(QSqlDatabase::contains("qt_sql_default_connection"))
        {
            database=QSqlDatabase::database("qt_sql_default_connection");
        }
        else
        {
            database=QSqlDatabase::addDatabase("QSQLITE");
            database.setDatabaseName("D:\\QT\\QTproject\\program\\wxx.db");
        }
}

bool mySql::openBase()
{
    if(!database.open())
    {
        qDebug("open base error");
        return false;
    }
    else
    {
        qDebug("open success");
        return true;
    }
}

bool mySql::isExist(QString &tableName)
{

    if(database.tables().contains(tableName))
    {
        qDebug("表格已存在");
        return true;
    }
    else
    {
        return false;
    }
}

void mySql::commonFuc(QString &command)
{
    QSqlQuery query;
    if(!query.exec(command))
    {
        qDebug()<<query.lastError();
    }
    else
    {
        qDebug()<<"执行成功";
    }
}

void mySql::closeBase()
{
    database.close();
    qDebug("close base");
}

bool mySql::isRegistered(QString &account)
{
    QSqlQuery query;
    query.prepare("SELECT COUNT(*) FROM userInfo WHERE account=?");
    query.addBindValue(account);
    if(!query.exec())
    {
        qDebug()<<query.lastError();
    }
    else
    {
        if(query.next())
        {
            int num=query.value(0).toInt();
            if(num>0)
            {
                return true;
            }
            else
            {
                return false;
            }
                
        }
    }
    return false;
}

bool mySql::isPassword(QString &account, QString &password)
{
    QSqlQuery query;
    query.prepare("SELECT COUNT(*) FROM userInfo WHERE account=? AND password=?");
    query.addBindValue(account);
    query.addBindValue(password);
    if(!query.exec())
    {
        qDebug()<<query.lastError();
    }
    else
    {
        if(query.next())
        {
            int num=query.value(0).toInt();
            if(num>0)
            {
                qDebug()<<"登录成功";
                return true;
            }
            else
            {
                qDebug()<<"密码错误";
                return false;
            }

        }
    }
    return false;
}

bool mySql::isCollected(QString &command)
{
    openBase();
    QSqlQuery query;
    if(!query.exec(command))
    {
        qDebug()<<"error:"<<query.lastError();
        return false;
    }
    else
    {
        while(query.next())
        {
            return true;
        }
        return false;
    }
    closeBase();
}

void mySql::findCityCode(QString &command, QString &code)
{
    openBase();
    QSqlQuery query;
    if(!query.exec(command))
    {
        qDebug()<<"error:"<<query.lastError();
    }
    else
    {
        while(query.next())
        {
           code=query.value(0).toString();
        }

    }
    closeBase();
}

void mySql::getAllWeather(QVector<weather> &vect)
{
    openBase();
    QString comand=QString("SELECT * FROM weatherInfo");
    QSqlQuery query;
    query.prepare(comand);
    if(!query.exec())
    {
        qDebug()<<query.lastError();
    }
    else
    {
        while(query.next())
        {
            QString date=query.value(0).toString();
            QString week=query.value(1).toString();
            QString type=query.value(2).toString();
            int low=query.value(3).toString().mid(0,3).toInt();
            int high=query.value(4).toString().mid(0,3).toInt();
            int aqi=query.value(5).toString().toInt();
            weather day(date,week,type,low,high,aqi);
            vect.push_back(day);
        }
    }
    closeBase();
}

bool mySql::getAppInfo(QString &appName, appInfo &info)
{
    openBase();
    QString comand=QString("SELECT * FROM appStore WHERE name=?");
    QSqlQuery query;
    query.prepare(comand);
    query.addBindValue(appName);
    if(!query.exec())
    {
        qDebug()<<query.lastError();
    }
    else
    {
        while(query.next())
        {
            info.name=query.value(0).toString();
            info.memory=query.value(1).toString();
            info.comment=query.value(2).toString();
            info.path=query.value(3).toString();
            return true;
        }
    }
    return false;
    closeBase();
}

